//
//  BaseTitleView.m
//  xiuPai
//
//  Created by apple on 16/10/20.
//  Copyright © 2016年 JLXX--YZG. All rights reserved.
//

#import "BaseTitleView.h"
#import "JLXXDefine.h"
#import "UIView+JLXXFrame.h"
#import "UIColor+JLXXHex.h"
@interface BaseTitleView ()

@property (nonatomic , strong) UIButton *selecedButton;
@property (nonatomic , assign) CGFloat defaultButtonFontSize;
@property (nonatomic , strong) UIView *underLine;
@property (nonatomic , weak) UIView *underLineSuperView;
@end

@implementation BaseTitleView

+(instancetype)titleViewFromXib{
    return [[[NSBundle bundleForClass:[self class]] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
}

-(void)setButtonArray:(NSArray<UIButton *> *)buttonArray{
    _buttonArray = buttonArray;
    for (UIButton *button in _buttonArray) {
        [button addTarget:self action:@selector(titleViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//        [button setTitleColor:kNavDarkColor forState:UIControlStateNormal];
        
        if (!self.underLineSuperView) {
            self.underLineSuperView = button.superview;
            self.defaultButtonFontSize = button.titleLabel.font.pointSize;
        }
    }
}

-(void)selectButtonForTag:(NSInteger)tag{
    UIButton *button = self.buttonArray[tag];
    [self titleViewButtonClick:button];
}

#pragma mark -- titleView上的按钮的点击事件
- (void)titleViewButtonClick:(UIButton *)button{
    if (self.selecedButton == button) return;
    self.selecedButton.selected = NO;
//    self.selecedButton.titleLabel.font = [UIFont systemFontOfSize:self.defaultButtonFontSize];
//    [self.selecedButton setTitleColor:kNavDarkColor forState:UIControlStateNormal];
    self.selecedButton = button;
    self.selecedButton.selected = YES;
//    self.selecedButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold"  size:self.defaultButtonFontSize];
//    [self.selecedButton setTitleColor:[self selectedColorOrDefaultColor] forState:UIControlStateSelected];
    
    [self changeUnderLineXWithButton:button];
    //传出当前选中按钮的tag
    if (self.selectedButtonTag)
    {
        self.selectedButtonTag(button.tag);
    }
}

-(void)changeUnderLineXWithButton:(UIButton *)button
{
    self.underLine.frame = CGRectMake(button.x, self.underLineSuperView.height - 3, button.width, 3);
}

-(UIView *)underLine{
    if (!_underLine) {
        NSAssert(self.buttonArray, @"buttonArray没有赋值..请在初始化该titleView后赋值");
        _underLine = [[UIView alloc] init];
        [self.underLineSuperView  addSubview:_underLine];
        self.underLine.backgroundColor = [self selectedColorOrDefaultColor];
    }
    return _underLine;
}

-(UIColor *)selectedColorOrDefaultColor{
    if (!self.selectedColor) {
        return  kNavColor;
    }
    return self.selectedColor;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.underLine.frame = CGRectMake(self.selecedButton.x, self.underLineSuperView.height - 3, self.selecedButton.width, 3);
    
}
@end
